package com.study.easyexcel.common.valid;

import com.study.easyexcel.common.constant.ExcelConstants;
import com.study.easyexcel.common.exception.CheckedException;
import org.springframework.util.ObjectUtils;

import java.util.regex.Pattern;

/**
 * 值效验工具
 *
 * @author yuhuan
 * @date 2023/6/28
 */
public class ValidationProcedure {

    /**
     * 空值校验
     *
     * @param nickname 字段的昵称
     * @param fv       要验证的值
     * @param errMsg   未通过校验的异常消息
     * @throws CheckedException 值为空
     */
    public void notNull(String nickname, Object fv, String errMsg) throws CheckedException {
        boolean empty = ObjectUtils.isEmpty(fv);
        if (empty) {
            if (errMsg.length() == 0) {
                errMsg = nickname + "不能为空";
            } else {
                errMsg = errMsg.replace(ExcelConstants.EXCEL_MSG_SEAT, String.valueOf(fv));
            }
            throw new CheckedException(errMsg);
        }
    }

    /**
     * 字符长度校验
     *
     * @param nickname 字段的昵称
     * @param fv       要验证的值
     * @param maxLen   最大字符长度
     * @param minLen   最小字符长度
     * @param errMsg   未通过校验的异常消息
     * @throws CheckedException 字符长度不符合绑定器规定的值
     */
    public void charSize(String nickname, Object fv, Integer maxLen, Integer minLen, String errMsg) throws CheckedException {
        int length = errMsg.length();
        // 替换消息中的占位符
        errMsg = length == 0 ? errMsg : errMsg.replace(ExcelConstants.EXCEL_MSG_SEAT, String.valueOf(fv));
        if (fv != null && String.valueOf(fv).length() > maxLen) {
            if (length == 0) {
                errMsg = nickname + "最大长度不能超过" + maxLen;
            }
            throw new CheckedException(errMsg);
        }
        if (fv != null && String.valueOf(fv).length() < minLen) {
            if (length == 0) {
                errMsg = nickname + "最小长度不能低于" + minLen;
            }
            throw new CheckedException(errMsg);
        }
    }

    /**
     * 正则匹配校验
     *
     * @param nickname 字段的昵称
     * @param fv       要验证的值
     * @param regexes  正则表达式
     * @param errMsg   未通过校验的异常消息
     * @throws CheckedException 字符不符合绑定器规定的正则匹配规则
     */
    public void pattern(String nickname, Object fv, String regexes, String errMsg) {
        Pattern compile = Pattern.compile(regexes);
        if (fv != null && !compile.matcher(String.valueOf(fv)).find()) {
            if (errMsg.length() == 0) {
                errMsg = nickname + "值不符合预期";
            } else {
                errMsg = errMsg.replace(ExcelConstants.EXCEL_MSG_SEAT, String.valueOf(fv));
            }
            throw new CheckedException(errMsg);
        }
    }

    public static ValidationProcedure build() {
        return new ValidationProcedure();
    }

    public static void notNull(String nickname, Object fv) throws CheckedException {
        build().notNull(nickname, fv, "");
    }

    public static void charSize(String nickname, Object fv, Integer maxLen, Integer minLen) throws CheckedException {
        build().charSize(nickname, fv, maxLen, minLen, "");
    }

    public static void pattern(String nickname, Object fv, String regexes) {
        build().pattern(nickname, fv, regexes, "");
    }
}
